# Contributor: Francesco Colista <fcolista@alpinelinux.org>
# Maintainer: Francesco Colista <fcolista@alpinelinux.org>
pkgname=icingaweb2
pkgver=2.12.1
pkgrel=1
pkgdesc="Web Interface for Icinga2"
url="https://icinga.com/docs/icinga-web-2/latest/doc/01-About/"
# x86: php6-pecl-imagick missing
arch="noarch !armhf !armv7 !s390x !x86"
license="GPL-2.0-only"
_php=php82
depends="$_php $_php-intl $_php-dom
	$_php-opcache $_php-session $_php-curl
	$_php-gettext $_php-ctype $_php-openssl $_php-sockets
	icinga-php-library icinga-php-thirdparty"
pkgusers="icingaweb2"
pkggroups="icingaweb2"
options="!check"
install="$pkgname.pre-install $pkgname.post-install"
subpackages="$pkgname-doc
	$pkgname-bash-completion:bashcomp:noarch
	$pkgname-sqlite-backend:backend_sqlite
	$pkgname-mysql-backend:backend_mysql
	$pkgname-postgres-backend:backend_postgres
	$pkgname-ldap:ldap
"
source="$pkgname-$pkgver.tar.gz::https://github.com/Icinga/icingaweb2/archive/refs/tags/v$pkgver.tar.gz
fix-env-php8.patch"

# secfixes:
#   2.9.0-r0:
#     - CVE-2021-32746
#     - CVE-2021-32747

package() {
	mkdir -p "$pkgdir/etc/$pkgname"
	mkdir -p "$pkgdir/usr/bin"
	mkdir -p "$pkgdir/usr/share/webapps/$pkgname"
	mkdir -p "$pkgdir/var/log/$pkgname"
	mkdir -p "$pkgdir/usr/share/doc/$pkgname"
	cp -r application doc library modules public bin etc schema "$pkgdir"/usr/share/webapps/$pkgname
	ln -s /usr/share/webapps/icingaweb2/bin/icingacli "$pkgdir"/usr/bin/icingacli
	chmod 2770 "$pkgdir"/etc/$pkgname
	chgrp -R $pkggroups "$pkgdir"/etc/$pkgname
	chmod 750 "$pkgdir"/var/log/$pkgname

	install -d -g $pkggroups -m 0775 "$pkgdir"/var/lib/$pkgname
	install -Dm644 LICENSE "$pkgdir/usr/share/licenses/icingaweb2/LICENSE"

	cat >"$pkgdir"/usr/share/doc/$pkgname/README.alpine <<EOF
You need to add to $pkggroups group the owner of the user of your webserver

For nginx, as example:
	# adduser nginx $pkgusers
or
	#adduser nobody $pkgusers

For Apache:
	# adduser apache $pkgusers

For lighttpd:
	# adduser lighttpd $pkgusers

Also, you may remove the user of your webserver from group 'icingacmd' if no other icinga web interface is installed.
EOF
}

bashcomp() {
	depends="bash"
	pkgdesc="Bash completions for $pkgname"

	cd "$builddir"
	mkdir -p "$subpkgdir"/usr/share/bash-completion/completions
	mv etc/bash_completion.d/icingacli "$subpkgdir"/usr/share/bash-completion/completions/icingacli
}

backend_sqlite() {
	depends="$pkgname $_php-sqlite3 $_php-pdo_sqlite"
	pkgdesc="Icingaweb2 sqlite3 backend"
	mkdir -p "$subpkgdir"
}

backend_postgres() {
	depends="$pkgname $_php-pgsql $_php-pdo_pgsql"
	pkgdesc="Icingaweb2 postgresql backend"
	mkdir -p "$subpkgdir"/usr/share/webapps/icingaweb2/schema
	mv "$pkgdir"/usr/share/webapps/$pkgname/schema/pg* "$subpkgdir"/usr/share/webapps/$pkgname/schema
}

backend_mysql() {
	depends="$pkgname $_php-pdo_mysql"
	pkgdesc="Icingaweb2 mysql backend"
	mkdir -p "$subpkgdir"/usr/share/webapps/icingaweb2/schema
	mv "$pkgdir"/usr/share/webapps/$pkgname/schema/my* "$subpkgdir"/usr/share/webapps/$pkgname/schema
}

ldap() {
	depends="$pkgname $_php-ldap"
	pkgdesc="Icingaweb2 ldap support"
	mkdir -p "$subpkgdir"
}

sha512sums="
71e6ad8f947ee47ff0bd3924920584c558fbd8e371f76adb83cc08e95442e792009af79280c484afe478cf1a176cf2812c6be661d838f61f044abdc106ca0999  icingaweb2-2.12.1.tar.gz
58ce086e90fa65cd9bae01c31b61f779978b532f905b12d5e850653bde5e8e84b7eb3a8d388f12427ea70128b6e14b55e38e9ba7027d0700c9b11f1452c7b10d  fix-env-php8.patch
"
